<?php 


class User extends xObject
 {
 
	function __construct()
	{		
		xObject::__construct(core::getWebSQL(),"mws_accounts") ;		
	}
	
	
 	
	function getByEmail($email)
	{
		$query = "SELECT SQL_CACHE id FROM ".$this->table_name." WHERE email = '".$this->sql_link->real_escape_string($email)."' ";
		$r = $this->sql_link->query($query);
		//echo $r->error;
		if ($r->num_rows == 0) return false ;
		else 
		{
			$data = $r->fetch_object();
			$this->getByKey($data->id);
			return true;
		}
	}
	
	
 	function getByUsername($login)
	{
		$query = "SELECT SQL_CACHE id FROM ".$this->table_name." WHERE username = '".$this->sql_link->real_escape_string($login)."' ";
		$r = $this->sql_link->query($query);
		//echo $r->error;
		if ($r->num_rows == 0) return false ;
		else 
		{
			$data = $r->fetch_object();
			$this->getByKey($data->id);
			return true;
		}
	}
 
	function getGameAccountInfos()
	{
		$sql = core::getAuthSQL();
		$query = "SELECT * FROM account WHERE id = '".$this->id."'";
		$r = $sql->query($query);
		$data = $r->fetch_object();
		return $data;
	}
	
 	function getBanStatus()
	{
		$sql = core::getAuthSQL();
		$Conn = $sql->query("SELECT * FROM account_banned WHERE id = '".$this->id."' AND active = 1 ORDER BY bandate DESC LIMIT 1");
		if($Conn->num_rows == 0) { return false;}
		else
		{
			$Data = $Conn->fetch_object();
			return $Data ;
		}
	}
	
	function getBanAccountHistoric()
	{
	$sql = core::getAuthSQL();
		$Conn = $sql->query("SELECT * FROM account_banned WHERE id = '".$this->id."' ORDER BY bandate DESC");
		if($Conn->num_rows == 0) { return false;}
		else
		{
			$tab = NULL;
			while($tmp = $Conn->fetch_object())
			{
				$tab[] = $tmp;
			}
			return $tab ;
		}
	}
	
	function getIPStatus()
	{
		$accinfos = $this->getGameAccountInfos();
		$sql = core::getAuthSQL();
		$query = "SELECT * FROM ip_banned WHERE ip = '".$accinfos->last_ip."' AND (unbandate >= UNIX_TIMESTAMP() OR bandate= unbandate) ORDER BY bandate DESC LIMIT 1";
		$Conn = $sql->query($query);
		if($Conn->num_rows == 0) { return false;}
		else
		{
			$Data = $Conn->fetch_object();
			return $Data ;
		}
	}
	
	function getGMInfos()
	{
		$sql = core::getAuthSQL();
		$sql->select_db(MYSQL_AUTH_DB);
		$query = "SELECT SQL_CACHE * FROM account_access WHERE id = '".$this->id."' ";
		$r = $sql->query($query);
		
		if ($r->num_rows == 0) return false;
		else
		{ 	
			return  $r->fetch_object();
		}
	}
	
	function getCharacters($mode = "tab")
	{
		$sql = core::getWorldSQL();
		$sql->select_db(MYSQL_WORLD_DB_CHARACTERS);
		$r = $sql->query("SELECT SQL_CACHE guid FROM characters WHERE account = '".$this->id."' ");
		if ($r->num_rows == 0) return false;
		else
		{ 	
			$tab = NULL;
			$str = "";
			while($tmp = $r->fetch_object())
			{
				$tmpChar = new Character();
				$tmpChar->getByKey($tmp->guid, "guid");
				switch($mode)
				{
					case "tab":
					{
						$tab[] = $tmpChar;
					}
					break;
					case "strings":
					{
						$str.= "<a href='javascript:;'  id='character-".$tmpChar->guid."'>".utf8_encode($tmpChar->name)." (Niv ".$tmpChar->level.")</a>";
					}
					break;
				}
			}
			
			switch($mode)
				{
					case "tab":
					{
						return $tab;
					}
					break;
					case "strings":
					{
						return $str;
					}
					break;
				}
		}
	}
	
	function getHTMLCharList()
	{
		
	}
	
	function getCurrentOnlineCharacter()
	{
		$sql = core::getWorldSQL();
		$sql->select_db(MYSQL_WORLD_DB_CHARACTERS);
		$query = "SELECT SQL_CACHE * FROM characters WHERE account='".$this->id."' AND online = '1' ";
		
		$r = $sql->query($query);
		if(!$r->num_rows) return false;
		else
		{
			return $r->fetch_object();
		}
	}
	
 	function getOfflineCharacters($faction="both")
	{
		$sql = core::getWorldSQL();
		$sql->select_db(MYSQL_WORLD_DB_CHARACTERS);
		$races="";
		switch($faction)
		{
			case "both":
				{
					$races = "";
					break;
				}
			case "a2":
				{
					$races = "AND race IN(1,3,4,7,11)";
					break;
				}
			case "h2":
				{
					$races = "AND race IN(2,5,6,8,10)";
					break;
				} 
		}
		
		$r = $sql->query("SELECT SQL_CACHE guid FROM characters WHERE account = '".$this->id."' ".$races." AND online = 0");
		if ($r->num_rows == 0) return false;
		else
		{
			$tab = NULL;
			while($tmp = $r->fetch_object())
			{
				$tmpChar = new Character();
				$tmpChar->getByKey($tmp->guid, "guid");
				$tab[] = $tmpChar;
			}
			
			return $tab;
		}
	}
	
	//**********  
	// Liste de personnages HORS LIGNE / ou pas non marqués par le flag
	function getUnmarkedAtLoginCharacters($online=0)
	{
		$sql = core::getWorldSQL();
		$sql->select_db(MYSQL_WORLD_DB_CHARACTERS);
		$q = "SELECT SQL_CACHE guid FROM characters WHERE account = '".$this->id."' AND at_login = 0 AND online = ".$online."";
		//debug($q);
		$r = $sql->query($q);
		if ($r->num_rows == 0) return false;
		else
		{
			$tab = NULL;
			while($tmp = $r->fetch_object())
			{
				$tmpChar = new Character();
				$tmpChar->getByKey($tmp->guid, "guid");
				$tab[] = $tmpChar;
			}
			
			return $tab;
		}
	}
	function setPassword($newpassword)
	{
		$sqlSite = core :: getWebSQL();
		$sqlGame = core :: getAuthSQL();
		$passwdIncr  = 0;
		
		$GamePass = sha_keyCrypt(strtolower($this->username), $newpassword);
		$Pass= sha1($newpassword);
		$q1 = "UPDATE account SET sha_pass_hash = '".$sqlGame->real_escape_string($GamePass)."' , sessionkey = 0, v = 0, s =0 WHERE id = '".$this->id."' ";
		
		if ($sqlGame->query($q1)) 
		{
			$passwdIncr++;
		}
		else
		{
			echo "ERR: ".$sqlGame->error;
			exit(0);
		}
		
		if ($sqlSite->query("UPDATE site_accounts SET sha_pass_hash = '".$sqlSite->real_escape_string($GamePass)."' WHERE id = '".$this->id."' ")) 
		{
			$passwdIncr++; 
		}
		else
		{
			echo "ERR: ".$sqlSite->error;
			exit(0);
		}
		
		if ($sqlSite->query("UPDATE forum_users SET user_password = '".$sqlSite->real_escape_string($Pass)."' WHERE user_id = '".$this->id."' ")) 
		{ 
			$passwdIncr++; 
		}
		else
		{
			echo "ERR: ".$sqlSite->error;
			exit(0);
		}
		 
		if($passwdIncr == 3)
		{
			return true;
		}
		else
		{
			return false;
		}
		
		
	
		return false;
	}
	
	function setSessVal($varName, $varVal)
	{
		
			$_SESSION[$varName] = $varVal;
		
	}
	
	function getSessVal($varName)
	{
		
			if($_SESSION[$varName]) return  $_SESSION[$varName] ;
			else return false;
		
	}
	
	function delSessVal($varName)
	{
		
			if($_SESSION[$varName]) unset($_SESSION[$varName]) ;
			else return false;
		
	}
	
	function canAfford($val)
	{
		if($this->current_vote_credit < $val) return false;
		else return true ;
	}
	
	function debitPV($points)
	{
		$this->current_vote_credit = $this->current_vote_credit - $points;
		$this->lock();
		$this->unlock_attribute("current_vote_credit");
		$this->write();
	}
	
 	function creditPV($points)
	{
		$this->current_vote_credit = $this->current_vote_credit + $points;
		$this->lock();
		$this->unlock_attribute("current_vote_credit");
		$this->write();
	}
	
	  function getAvatarURL()
	 {
	 		
			$ObjConn = $this->sql_link->query("SELECT user_avatar FROM forum_users WHERE user_id ='".$this->id."' ");
			$URL = $ObjConn->fetch_object();
			 
			if($URL->user_avatar == "")
			{
				return "img/icons/avatar_anonymous.gif";
			}
			else
			{
				return $URL->user_avatar;
			}
			
	 }
	 
	 function addAllopass($code, $transactCode)
	 {
	 	//credit de points
	 	$points = core::getSiteConfVal("allopass_pbc");
	 	$this->creditPV($points);
	 	//Sauvegarde de l'allopass
	 	$allopass = new Allopass();
	 	$allopass->create();
	 	$allopass->code = $code ;
	 	$allopass->date = date("Y-m-d H:i:s", time()) ;
	 	$allopass->account_id = $this->id ;
	 	$allopass->transaction_code = $transactCode ;
	 	$allopass->unlock();
	 	$allopass->write();
	 }
	 
	 function canVoteFor($siteID)
	 {
	 	
	 	switch($siteID)
	 	{
	 		case TOPSITE_TOPWOW :
	 			{
	 				if(($this->vote_timer1 + 7200) <= time()) return true;
	 				else return false;
	 			}
	 			break;
	 		case TOPSITE_RPG :
	 			{
	 				if(($this->vote_timer2 + 7200) <= time()) return true;
	 				else return false;
	 			}
	 		break;
	 		
	 		default:
	 			{
	 				return false;
	 			}
	 			break;
	 	}
	 }
	 
	 function makeVoteFor($siteID) //ATTENTION _ REDUIT A  1 SITE !!!!
	 {
	 	//on vote = credit +1 && timer = time
	 	
	 	$requiredVotes = core::getSiteConfVal("required_votes_to_convert");
	 	//on augmente le votebuffer
	 	$this->tmp_votebuffer++;
	 	
	 	if(!($this->tmp_votebuffer ==  $requiredVotes))
	 	{
	 		//On enregistre simplement le vote
		 	switch($siteID)
		 	{
		 		case TOPSITE_RPG :
		 			{
		 				$this->vote_timer2 = time();
		 				$this->month_votes++;
		 				$this->lock();
		 				$this->unlock_attribute("tmp_votebuffer");
		 				$this->unlock_attribute("vote_timer2");
		 				$this->unlock_attribute("month_votes");
		 				$this->write();
		 				return TOPSITE_RPG;
		 			}
		 		break;
		 		
		 		default:
		 			{
		 				return false;
		 			}
		 			break;
		 	}
	 	}
	 	else
	 	{
	 		//On remet le compteur a 0 et on donne 1 KSP !
	 		$this->tmp_votebuffer = 0 ;
		 	switch($siteID)
			 	{
			 		case TOPSITE_RPG :
			 			{
			 				$this->current_vote_credit++;
			 				$this->vote_timer2 = time();
			 				$this->month_votes++;
			 				$this->lock();
			 				$this->unlock_attribute("current_vote_credit");
			 				$this->unlock_attribute("tmp_votebuffer");
			 				$this->unlock_attribute("vote_timer2");
			 				$this->unlock_attribute("month_votes");
			 				$this->write();
			 				return TOPSITE_RPG;
			 			}
			 		break;
			 		
			 		default:
			 			{
			 				return false;
			 			}
			 			break;
			 	}
	 	}
	 	
	 	
	 
	 }
	 
	 
	 function getShopOrdersHistoric()
	 {
	 	$query ="SELECT SQL_CACHE * FROM site_shop_orders WHERE account = '".$this->id."' ORDER BY `date` DESC";
	 	$r = $this->sql_link->query($query);
	 	if(!$r->num_rows) return false;
	 	else
	 	{
	 		$tab = null;
	 		while($tmp = $r->fetch_object())
	 		{
	 			$tab[] =$tmp;
	 		}
	 		return $tab;
	 	}
	 }
	 
	 function getAllopassHistoric()
	 {
	 	$query ="SELECT SQL_CACHE * FROM site_allopass WHERE account_id = '".$this->id."' ORDER BY `date` DESC";
	 	$r = $this->sql_link->query($query);
	 	if(!$r->num_rows) return false;
	 	else
	 	{
	 		$tab = null;
	 		while($tmp = $r->fetch_object())
	 		{
	 			$tab[] =$tmp;
	 		}
	 		return $tab;
	 	}
	 }
 }

 

?>